package com.yanqu.road.server.cmd.blacklist;


import com.yanqu.road.pb.player.BlacklistProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BlacklistModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;


@Cmd(code = Protocol.S_BLACKLIST_ADD, desc = "设置黑名单")
public class AddBlacklistCmd extends AbstractCommand {
	private long blacklistUserServerId;
	private long blacklistUserId;
	private int type;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		BlacklistProto.BlacklistAddReqMsg reqMsg = BlacklistProto.BlacklistAddReqMsg.parseFrom(packet.getBytes());
		blacklistUserServerId = reqMsg.getBlacklistUserServerId();
		blacklistUserId = reqMsg.getBlacklistUserId();
		type = reqMsg.getType();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(GameServer.getInstance().getServerId() == blacklistUserServerId && player.getUserId() == blacklistUserId){
			ret = GameErrorCode.E_BLACKLIST_CAN_NO_SELF;
			return;
		}
		ret = player.getModule(BlacklistModule.class).addBlacklist(type, blacklistUserId, blacklistUserServerId);
	}

	@Override
	public void sendData(GamePlayer player) {
		if(0 != ret) {
			BlacklistProto.BlacklistAddRespMsg.Builder respMsg = BlacklistProto.BlacklistAddRespMsg.newBuilder();
			respMsg.setRet(ret);
			respMsg.setBlacklistUserId(blacklistUserId);
			respMsg.setBlacklistUserServerId(blacklistUserServerId);
			respMsg.setType(type);
			player.sendPacket(Protocol.U_BLACKLIST_ADD, respMsg);
		}
	}
}
